<?php

if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Userinfo extends MY_Controller
{

    function __construct()
    {
        parent::__construct();
        if ( !$this->permission->chkPermission( 'info' ) )
        {
            $url = config_item( 'base_url' );
            redirect( $url );
        }
    }

    /**
     * 我的信息列表
     * @param int $offset 游标
     */
    public function index( $offset = 0 )
    {
        $data = array( );
        $this->get_common_section( $data );

        $data['infoList'] = Modules::run( 'info/userInfo', $offset );

        $this->load->view( 'info/list', $data );
    }

    /**
     * 刷新信息
     */
    public function refresh( $postId )
    {
        $data = array( );
        $this->get_common_section( $data );

        $postData = $this->model_post_getById( $postId );
        if ( empty( $postData ) || $this->session->getUserSession( 'id' ) != $postData['userid'] )
        {
            //提示有误
            $messageData = array(
                'title' => '操作有误',
                'message' => "4秒后自动跳转回我的信息",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'userinfo', '', '', false ),
            );
        }
        else
        {
            $this->model_post_refreshByPostId( $postId );
            //提示有误
            $messageData = array(
                'title' => '信息刷新成功',
                'message' => "4秒后自动跳转回我的信息",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'userinfo', '', '', false ),
            );
        }
        $messageBox = Modules::run( 'messageBox', $messageData );
        $data['message'] = $messageBox;

        $this->load->view( 'info/refresh', $data );
    }

    public function delete( $postId )
    {
        $data = array( );
        $this->get_common_section( $data );

        $postData = $this->model_post_getById( $postId );
        if ( empty( $postData ) || $this->session->getUserSession( 'id' ) != $postData['userid'] )
        {
            //提示有误
            $messageData = array(
                'title' => '操作有误',
                'message' => "4秒后自动跳转回我的信息",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'userinfo', '', '', false ),
            );
        }
        else
        {
            $attachMentData = $this->model_attachment_getByPostId( $postId );
            $this->model_post_deleteByPostId( $postId );
            $this->model_postattachment_deleteByPostId( $postId );
            $this->model_attachment_deleteById( $attachMentData['id'] );
            $this->model_comment_deleteByPostId( $postId );
            if ( $attachMentData )
            {
                //如果存在附件查询相关附件数据
                $attachMentFile = $attachMentData['path'] . $attachMentData['name'];
                @unlink( $attachMentFile );
            }

            //提示有误
            $messageData = array(
                'title' => '信息删除成功',
                'message' => "4秒后自动跳转回我的信息",
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'userinfo', '', '', false ),
            );
        }
        $messageBox = Modules::run( 'messageBox', $messageData );
        $data['message'] = $messageBox;

        $this->load->view( 'info/delete', $data );
    }

    /**
     * 修改指定信息内容
     * @param int $postId 信息ID
     */
    public function edit( $postId )
    {
        $postData = $this->model_post_getById( $postId );
        if ( empty( $postData ) )
        {
            $this->index();
        }
        else
        {
            $data = array(
                'title' => "修改{$postData['title']}信息",
            );
            $this->get_common_section( $data );
            $rentForm = ($postData['type'] <= 2) ? Modules::run( 'postForm/rentEdit', $postId ) : Modules::run( 'postForm/sellEdit', $postId );
            $data['rentForm'] = $rentForm;
            $this->load->view( 'info/edit', $data );
        }
    }

    public function editsubmit()
    {
        if ( chk_permission( 'publishrent' ) && $this->input->post( 'submit' ) && $this->form_validation->valid_token() )
        {
            $postId = intval( $this->input->post( 'postid' ) );
            $postData = $this->model_post_getById( $postId );
            $postRentData = ($postData['type'] <= 2) ? Modules::run( 'postForm/rentSubmit' ) : Modules::run( 'postForm/sellSubmit' );
            if ( $postRentData )
            {
                if ( isset( $_FILES['image'] ) && $_FILES['image'] )
                {
                    //修改图片
                    $this->load->library( 'upload', get_upload_config() );
                    if ( !$this->upload->do_upload( 'image' ) )
                    {
                        //上传图片失败
                    }
                    else
                    {
                        //如果有旧图片要删除掉
                        $postImg = $this->model_attachment_getByPostId( $postId );
                        if ( $postImg )
                        {
                            //删除图片
                            $this->model_attachment_deleteById( $postImg['id'] );

                            //删除图片信息关联表
                            $this->model_postattachment_deleteByPostId( $postId );

                            //删除图片
                            @unlink( $postImg['path'] . $postImg['name'] );
                        }
                        //获取上传数据
                        $uploadData = $this->upload->data();
                        $postRentData['upload'] = $uploadData;
                    }
                }

                $this->model_post_trans_begin();
                //更新post表
                $this->model_post_update( $postRentData, $postId );
                //如果有图片
                if ( isset( $postRentData['upload'] ) )
                {
                    $attachId = $this->model_attachment_add( $postRentData['upload'] );
                    $this->model_postattachment_add( array( 'postid' => $postId, 'attachid' => $attachId ) );
                }
                if ( $this->model_post_trans_status() === FALSE )
                {
                    $this->model_post_trans_rollback();
                    $rentForm = Modules::run( 'postForm/rentEdit', $postId );
                    $data = array(
                        'title' => '修改信息',
                        'rentForm' => $rentForm,
                        'error' => '系统出现错误，请重试看看',
                    );
                    $this->load->view( 'userinfo/edit', $data );
                }
                else
                {
                    $this->model_post_trans_commit();
                    //提示注册成功
                    $messageData = array(
                        'title' => '修改成功',
                        'message' => "信息修改成功，4秒后跳转回该信息页面",
                        'autoRedirect' => true,
                        'autoRedirectTimeOut' => 4,
                        'autoRedirectURL' => my_url( 'post', 'view', $postId, false ),
                    );
                    $messageBox = Modules::run( 'messageBox', $messageData );
                    $data = array(
                        'title' => '修改信息',
                        'rentForm' => $messageBox,
                    );
                    $this->get_common_section( $data );
                    $this->load->view( 'info/edit', $data );
                }
            }
            else
            {
                $this->edit( $postId );
            }
        }
        else
        {
            $this->index();
        }
    }

    /**
     * 获取指定信息的评论数
     */
    public function ajaxGetCommentNum()
    {
        if ( $this->input->is_ajax_request() )
        {
            //ajax请求
            $postIdsArr = explode( ',', $this->input->post( 'postids' ) );
            $result = array( );
            foreach ( $postIdsArr as $postId )
            {
                $result[] = array(
                    'id' => $postId,
                    'num' => intval( $this->model_comment_getNumByPostId( $postId ) ),
                );
            }
            sleep( 1 );
            echo json_encode( $result );
        }
    }

    public function comment( $postId )
    {
        $postData = $this->model_post_getById( $postId );
        if ( $postData['userid'] != $this->session->getUserSession( 'id' ) )
        {
            //提示
            $messageData = array(
                'title' => '错误',
                'message' => '非法操作，4秒后跳转回个人信息中心',
                'autoRedirect' => true,
                'autoRedirectTimeOut' => 4,
                'autoRedirectURL' => my_url( 'userinfo', '', '', false ),
            );
        }
        else
        {
            $postComment = $this->model_comment_getByPostId( $postId );
            $replyComment = array();
            foreach( $postComment as $single )
            {
                if ( $single['isapply'] )
                {
                    $replyComment[$single['pid']] = $single;
                }
            }
            $data['postData'] = $postData;
            $data['postComment'] = $postComment;
            $data['replyComment'] = $replyComment;
        }
        $this->get_common_section( $data );
        $this->load->view( 'info/comment', $data );
    }
    
    public function ajaxReplyComment()
    {
        if ( $this->input->is_ajax_request() )
        {
            //ajax请求
            $commentId = intval( $this->input->post('commentid') );
            $commentData = $this->model_comment_getById( $commentId );
            $userId = $this->session->getUserSession('id');
            if ( empty( $commentData ) || empty( $userId ) ) exit("0");
            $reply = $this->input->post('reply');
            $replyData = array(
                'postid' => $commentData['postid'],
                'userid' => $userId,
                'pid' => $commentId,
                'content' => $reply,
            );
            $result = $this->model_comment_reply( $replyData );
            sleep( 1 );
            echo 1;
        }
    }

}